package com.pateo.bjev.bjevvehicle.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;

import com.pateo.bjev.bjevvehicle.bean.PowerCost;
import com.pateo.bjev.bjevvehicle.dummy.Dummy;
import com.pateo.bjev.bjevvehicle.dummy.PowerCostCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pateo on 17-5-31.
 */

public class PowerCostView extends View {
    Paint mPaint;
    Paint mPaint2;

    Point o;//原点
    int stepX;//每格间距
    int stepY;

    float rateX;//x方向长度比率
    float rateY;//有方向长度比率
    private int marginToLine = 50;//标注离坐标轴的距离
    private Point[] marksX;//x轴标注
    private Point[] marksY;//y轴标注

    private List<Record> records = new ArrayList<>();
    private int height = 400;


    public PowerCostView(Context context) {
        super(context, null);
    }

    public PowerCostView(Context context, AttributeSet attrs) {
        super(context, attrs);
        rateX = 1;
        rateY = (float) height / 1000;
        initPaint();
        initOrigin();
        initStep();
        initmark();

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Dummy.reqRecord(new PowerCostCallback() {

            @Override
            public void callback(List<PowerCost> powerCosts) {
                bindData(powerCosts);
            }
        });
    }

    private void initmark() {
        marksX = new Point[6];
        for (int i = 0; i < marksX.length; i++) {
            marksX[i] = new Point(o.x + stepX * i, o.y + marginToLine);
        }
        marksY = new Point[6];
        for (int i = 0; i < marksX.length; i++) {
            marksY[i] = new Point(o.x - marginToLine, o.y - stepY * i);
        }
    }

    private void initStep() {
        stepX = 1000 / 5;
        stepY = (int) (1000 / 5 * rateY);
    }

    private void initOrigin() {
        o = new Point(100, 400);
    }

    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(2);


        mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint2.setColor(Color.GREEN);
        mPaint2.setStrokeWidth(2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawAxi(canvas);
        drawMarks(canvas);
        drawPoints(canvas);
        drawLines(canvas);//画连线
    }

    private void drawLines(Canvas canvas) {
        for (int i = 0; i < records.size(); i++) {
            if (i > 0) {
                Record start = records.get(i - 1);
                Record end = records.get(i);
                canvas.drawLine(start.x, start.y, end.x, end.y, mPaint2);
            }
        }
    }

    private void drawPoints(Canvas canvas) {
        for (int i = 0; i < records.size(); i++) {
            canvas.drawPoint(records.get(i).x, records.get(i).y, mPaint2);
        }
    }

    private void drawMarks(Canvas canvas) {
        for (int i = 0; i < marksX.length; i++) {
            canvas.drawText(String.valueOf(i * 20), marksX[i].x, marksX[i].y, mPaint);
        }
        for (int i = 0; i < marksY.length; i++) {
            canvas.drawText(String.valueOf((float) (i * 0.2)), marksY[i].x, marksY[i].y, mPaint);
        }
    }

    private void drawAxi(Canvas canvas) {
        canvas.drawLine(o.x, o.y, o.x + 1000 * rateX, o.y, mPaint);
        canvas.drawLine(o.x, o.y, o.x, o.y - 1000 * rateY, mPaint);
    }

    public void bindData(List<PowerCost> powercosts) {
        for (PowerCost powercost : powercosts) {
            float powers = powercost.getPowercost();
            int miles = powercost.getMiles();
            records.add(new Record(o.x + miles * 10 * rateX, o.y - powers * 1000 * rateY));
        }
        postInvalidate();
    }

    public class Record {
        float x;
        float y;

        public Record(float x, float y) {
            this.x = x;
            this.y = y;
        }
    }


}
